package com.lonwin.inspectionmanagement.mvp.presenter;

import android.Manifest;
import android.app.Application;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.ArrayMap;
import android.view.View;

import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.utils.PermissionUtil;
import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.service.LocationService;
import com.lonwin.inspectionmanagement.app.utils.AppUtil;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.HomeContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.HhkqLhAliasBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.QueryNumBean;
import com.lonwin.inspectionmanagement.mvp.ui.activity.HomeCruiseActivity;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.HomepopuwindowAdaptert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ActivityScope
public class HomePresenter extends BasePresenter<HomeContract.Model, HomeContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    private  ConfigBean configBean;
    String[] permission = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_NETWORK_STATE};
    @Inject
    public HomePresenter(HomeContract.Model model, HomeContract.View rootView) {
        super(model, rootView);
    }

    public ConfigBean getConfigBean() {
        return configBean;
    }

    public void setConfigBean(ConfigBean configBean) {
        this.configBean = configBean;
    }

    /**
     * 获取提示数据
     */
    public void getHzUnCheckXcjlbCount(){
        if (configBean!=null)
        {
            if (configBean.getCurrentRole().getRoleCode().equals(AppConstants.ROLE_LLY)||configBean.getCurrentRole().getRoleCode().equals(AppConstants.ROLE_LDBM))
            {
                //联络员
                Map<String,Object> maps=new ArrayMap<>();
                maps.put("groupids",AppUtil.getgroupids(configBean));
                mModel.getHzUnCheckXcjlbCount(DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/getBacklogXcjlbCount",maps).compose(RxUtils.applySchedulers(mRootView))
                        .subscribe(new ErrorHandleSubscriber<QueryNumBean>(mErrorHandler) {
                            @Override
                            public void onNext(QueryNumBean queryNumBean) {
                                if (queryNumBean!=null&&(queryNumBean.getData().getDoing()+queryNumBean.getData().getDone())>0)
                                {
                                    if (queryNumBean.getData().getDoing()>0)
                                    {
                                        mRootView.getCheckCount("您有 "+queryNumBean.getData().getDoing()+" 份巡查记录表待处理",queryNumBean.getData().getDoing());
                                    }else {
                                        mRootView.getCheckCount("查看历史巡查记录表",queryNumBean.getData().getDone());
                                    }
                                }else {
                                    mRootView.getCheckCount("",0);
                                }
                            }
                        });
            }else if (configBean.getCurrentRole().getRoleCode().equals(AppConstants.ROLE_HZ))
            {
                //河长
                Map<String,Object> maps=new ArrayMap<>();
                maps.put("hzid",configBean.getGpsId());
                mModel.getHzUnCheckXcjlbCount(DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/getHzUnCheckXcjlbCount",maps).compose(RxUtils.applySchedulers(mRootView))
                        .subscribe(new ErrorHandleSubscriber<QueryNumBean>(mErrorHandler) {
                            @Override
                            public void onNext(QueryNumBean queryNumBean) {
                                if (queryNumBean!=null&&(queryNumBean.getData().getDoing()+queryNumBean.getData().getDone())>0)
                                {
                                    if (queryNumBean.getData().getDoing()>0)
                                    {
                                        mRootView.getCheckCount("您有 "+queryNumBean.getData().getDoing()+" 份巡查记录表待签批",queryNumBean.getData().getDoing());
                                    }else {
                                        mRootView.getCheckCount("查看历史巡查记录表",queryNumBean.getData().getDone());
                                    }
                                }else {
                                    mRootView.getCheckCount("",0);
                                }

                            }
                        });
            }
        }
    }

    /**
     * 得到配置信息
     */
     public void getCondig(){
//         DataHelper.saveDeviceData(mRootView.getActivity(), AppConstants.EXTRA_CONFIG_DATA,null);
        if (mRootView.getActivity().getIntent().hasExtra("config"))
        {
             configBean=new Gson().fromJson(mRootView.getActivity().getIntent().getStringExtra("config"),ConfigBean.class);
            DataHelper.saveDeviceData(mRootView.getActivity(), AppConstants.EXTRA_CONFIG_DATA,configBean);
           mRootView.setConfig(configBean);
            return;
        }
          configBean=new Gson().fromJson(AppUtil.getConfigFile(mRootView.getActivity(),"default_config.json"),ConfigBean.class);
         DataHelper.saveDeviceData(mRootView.getActivity(), AppConstants.EXTRA_CONFIG_DATA,configBean);
         mRootView.setConfig(configBean);
     }
    /**
     * 查询河湖
     * @param projectid
     * @param adcd
     * @param status
     * @param gpsid
     * @param roleid
     */
    public void queryHhkqLhAlias(String projectid,String adcd, String status
            , String gpsid, String roleid){

        Map<String,String> maps=new HashMap<>();
        maps.put("projectid",projectid);
        maps.put("adcd",adcd);
        maps.put("status",status);
        maps.put("gpsid",gpsid);
        maps.put("roleid",roleid);

         mModel.queryHhkqLhAlias(DataUtil.getYWUrl(configBean)+"/api/v1/business/hzz/getHhkqLhAliasListByGpsID",maps).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<HhkqLhAliasBean>(mErrorHandler) {
                    @Override
                    public void onNext(HhkqLhAliasBean hhkqLhAliasBean) {
                        if (hhkqLhAliasBean==null||hhkqLhAliasBean.getData()==null||hhkqLhAliasBean.getData().isEmpty())
                        {
                           mRootView.showMessage("未获取到河流信息");
                            return;
                        }
                        mRootView.queryHhkqLhAliasSuccerss(hhkqLhAliasBean.getData());
                    }
                });
    }

    /**
     * 显示河流选择弹出
     * @param dataBeans
     * @param popuWindowsClick
     */
     public void showPopuwindow(List<HhkqLhAliasBean.DataBean> dataBeans,PopuWindowsClick popuWindowsClick)
     {

         DataUtil.showDialog(mRootView.getActivity(),dataBeans,popuWindowsClick);
     }

    /**
     * 请求权限
     */
    public void nextActivity() {

        //请求外部存储权限用于适配android6.0的权限管理机制
        PermissionUtil.requestPermission(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {
                mRootView.showMessage("请求定位权限失败");
            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {
                mRootView.showMessage("请同意定位权限");
            }
        }, mRootView.getRxPermissions(), mErrorHandler, permission);
         //mRootView.getActivity().startService(new Intent(mRootView.getActivity(), LocationService.class));
        AppUtil.startService(mRootView.getActivity(),new Intent(mRootView.getActivity(), LocationService.class));

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public interface PopuWindowsClick{
        void clickItem(HhkqLhAliasBean.DataBean dataBean,int selectId);
    }
}
